package com.web.core.async.redis;

import com.web.core.util.Const;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

/**
 * 广播刷新缓存
 * 
 * @author zhengj
 * @since 1.0 2016年11月17日
 */
@Component
public class CacheRefreshSender
{
	// 基础模块
	public static final String CONFIG_CACHE = "configCache";

	private static Logger logger = LoggerFactory.getLogger(CacheRefreshSender.class);

	@Autowired(required = false)
	@Qualifier("redisTemplate")
	private RedisTemplate<String, Object> redisTemplate;

	public CacheRefreshSender()
	{
	}

	public void send(String bean)
	{
		if (redisTemplate == null)
		{
			logger.warn("RedisTemplate 未实例化");
			return;
		}

		redisTemplate.convertAndSend("send_cache_refresh", bean);
		logger.info("发送刷新缓存消息[bean={}]", bean);
	}

	public void send(String bean, Integer id)
	{
		if (redisTemplate == null)
		{
			logger.warn("RedisTemplate 未实例化");
			return;
		}

		redisTemplate.convertAndSend("send_cache_refresh", bean + Const.CACHE_SPLIT + id);
		logger.info("发送更新缓存消息[bean={}, id={}]", bean, id);
	}

}
